package com.example.springboot.controller.user;

import com.example.springboot.constant.ResConstansts;
import com.example.springboot.entity.Follow;
import com.example.springboot.entity.Res;
import com.example.springboot.entity.dto.FollowDto;
import com.example.springboot.service.FollowService;
import com.example.springboot.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user_follows")
public class FollowController {
    @Autowired
    private FollowService followService;
    private JwtUtil jwtUtil;
    @PostMapping
    public Res<Void> follow(@RequestHeader(value = "Authorization", required = false) String token, @RequestBody FollowDto followDto) {
        if (token == null)
            return new Res<Void>().code(401).msg("未登录");
        String reqToken = token.substring(7);// 去掉 "Bearer " 前缀
        Integer followId=jwtUtil.getUserIdByToken(reqToken);
        followDto.setFollowId(followId);
        followService.follow(followDto);
        return new Res<Void>().code(200).msg("操作成功");
//        try {
//
//        } catch (Exception e) {
//            System.out.println(e);
//            return ResConstansts.SERVICE_ERROR;
//        }
    }
    @GetMapping("/{followedId}")
    public Res<Integer> getIsFollow(@RequestHeader(value = "Authorization", required = false) String token , @PathVariable Integer followedId) {
        if (token == null)
            return new Res<Integer>().code(401).msg("未登录").data(0);
        try {
            String reqToken = token.substring(7);// 去掉 "Bearer " 前缀
            Integer followId=jwtUtil.getUserIdByToken(reqToken);
            Follow follow =followService.getIsFollow(followId,followedId);
            if(follow==null || follow.getIsFollow()==0){
                return new Res<Integer>().code(200).msg("操作成功").data(0);
            }else{
                return new Res<Integer>().code(200).msg("操作成功").data(1);
            }
        } catch (Exception e) {
            System.out.println(e);
            return ResConstansts.SERVICE_ERROR;
        }
    }
}
